package both;


import java.awt.Toolkit;
import java.io.IOException;
import java.util.*;

public class Game {

    private Roster roster=new Roster();
    private ArrayList<Question> questionList=new ArrayList<Question>();
    private static double defaultPointValue;
    private static int defaultTime;
    private int currentQuestionIndex=0;
    public static double width=Toolkit.getDefaultToolkit().getScreenSize().getWidth();
    public static double height=Toolkit.getDefaultToolkit().getScreenSize().getHeight();

     /* Gets all answers
     * Checks that question text matches the current question
     * Checks that the given answer is correct
     * Adds to numCorrect for the team the member is part of
     * Call updateScore on team at end
     *
     */

    public void updateScores(HashMap<String, Answer> answers) throws IOException{
        Question current=questionList.get(currentQuestionIndex);
        String correctAnswer=current.getAnswerChoices().get(current.getCorrectAnswer());
        for(Team team:roster.teamsByScore()) {
            team.resetNumCorrect();
        }

        for(String ID:answers.keySet()){
            Contestant currentContestant=roster.getContestantByID(ID);
            if(currentContestant==null) {
                System.out.println("No record of contestant with ID "+ID);
                System.out.println("Dropping answer");
                continue;
            }
            Team contestantTeam=roster.getTeamByName(currentContestant.getTeamName());
            if(currentContestant==null) {
                System.out.println("No record of team with name "+currentContestant.getTeamName());
                System.out.println("Dropping answer");
                continue;
            }
            Answer answer=answers.get(ID);
            if((answer.getQuestionText().equalsIgnoreCase(current.getQuestionText()))) {
                if(answer.getAnswerText().equalsIgnoreCase(correctAnswer)) {
                    contestantTeam.tickNumCorrect();
                }
            }
        }

        for(Team team:roster.teamsByScore()) {
            team.updateScore(questionList.get(currentQuestionIndex));
        }
    }


    public Roster getRoster() {
        return roster;
    }

    public void setRoster(Roster roster) {
        this.roster = roster;
    }

    

    public ArrayList<Question> getQuestionList() {
        return questionList;
    }

    public void setQuestionList(ArrayList<Question> questionList) {
        this.questionList = questionList;
    }

    

    public static double getDefaultPointValue(){
        return defaultPointValue;
    }

    public static void setDefaultPointValue(double pointValue){
        defaultPointValue=pointValue;
    }


    public static int getDefaultTime(){
        return defaultTime;
    }

    public static void setDefaultTime(int newTime){
        defaultTime=newTime;
    }

    public int getCurrentQuestionIndex() {
        return currentQuestionIndex;
    }

    public void incrementQuestionIndex(){
        currentQuestionIndex++;
    }
    
    public void downQuestionIndex(){
        currentQuestionIndex--;
    }

    public void resetQuestionIndex(){
        currentQuestionIndex=0;
    }

    public Question getCurrentQuestion() {
        return getQuestionList().get(getCurrentQuestionIndex());
    }
}
